<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'add', 'delete', 'comSearch', 'quickSearch']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('shop.market.coupon.center.quick Search Fields') })"
        ></TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef">
            <template #effective_time>
                <el-table-column label="优惠券有效期" width="240" align="center">
                    <template #default="{ row }">
                        <div v-if="row.expiry_type == 1">
                            <div style="display: flex; flex-direction: column">
                                {{ $t('shop.market.coupon.center.start_time') }}：{{ row.start_time }}
                                <div style="display: flex; flex-direction: column">
                                    {{ $t('shop.market.coupon.center.end_time') }}：{{ row.end_time }}
                                </div>
                            </div>
                        </div>
                        <div v-else-if="row.expiry_type == 2">
                            领券当天开始 <el-tag type="success">{{ row.expiry_days }}</el-tag> 天内有效
                        </div>
                    </template>
                </el-table-column>
            </template>
            <template #active_time>
                <el-table-column label="活动时间" width="240" align="center">
                    <template #default="{ row }">
                        <div style="display: flex; flex-direction: column">
                            {{ $t('shop.market.coupon.center.start_time') }}：{{ row.active_start_time }}
                            <div style="display: flex; flex-direction: column">
                                {{ $t('shop.market.coupon.center.end_time') }}：{{ row.active_end_time }}
                            </div>
                        </div>
                    </template>
                </el-table-column>
            </template>
        </Table>

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'

defineOptions({
    name: 'shop/market/coupon/center',
})

const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons(['weigh-sort', 'delete'])

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/shop.market.coupon.Center/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('shop.market.coupon.center.id'), prop: 'id', align: 'center', width: 70, operator: false, sortable: 'custom' },
            {
                label: t('shop.market.coupon.center.coupon__name'),
                prop: 'coupon.name',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                minWidth: 120,
                showOverflowTooltip: true,
            },
            { label: t('shop.market.coupon.center.total_num'), prop: 'total_num', align: 'center', operator: 'RANGE', sortable: false, width: 120 },
            { label: t('shop.market.coupon.center.remain_num'), prop: 'remain_num', align: 'center', operator: 'RANGE', sortable: false, width: 120 },
            { label: t('shop.market.coupon.center.limit_num'), prop: 'limit_num', align: 'center', operator: 'RANGE', sortable: false, width: 120 },
            { label: t('shop.market.coupon.center.is_newuser'), prop: 'is_newuser', align: 'center', render: 'tag', operator: 'eq', sortable: false, replaceValue: { '1': t('shop.market.coupon.center.is_newuser 1'), '2': t('shop.market.coupon.center.is_newuser 2') } },
            {
                label: t('shop.market.coupon.center.expiry_time'),
                prop: 'expiry_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
                show: false,
            },
            {
                label: t('shop.market.coupon.center.active_time'),
                prop: 'active_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
                show: false,
            },
            {
                render: 'slot',
                slotName: 'active_time',
                operator: false,
            },
            {
                render: 'slot',
                slotName: 'effective_time',
                operator: false,
            },
            { label: t('Operate'), align: 'center', width: 150, render: 'buttons', buttons: optButtons, operator: false, fixed: 'right' },
        ],
        dblClickNotEditColumn: ['all'],
    },
    {
        defaultItems: { total_num: 1, limit_num: 1, expiry_type: 1, expiry_days: 1 },
    },
    {},
    {
        requestEdit({ res }) {
            const row = res.data.row
            baTable.form.items!.expiry_time = [row.start_time, row.end_time]
            baTable.form.items!.active_time = [row.active_start_time, row.active_end_time]
        },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss">
:deep(.cell) {
    // 最多两行，多余的显示省略
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: normal;
    word-wrap: break-word;
    word-break: break-all;
}
</style>
